package logic

import (
	"context"
	"fmt"
	"math/rand"
	"sends/models/redisinit"
	"time"

	"sends/sendrpc/internal/svc"
	"sends/sendrpc/sendrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *sendrpc.SendSmsRequest) (*sendrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	get := redisinit.Client.Get("Send" + in.Account + in.Source)
	if get.Val() != "" {
		return nil, fmt.Errorf("短信每分钟只能发送一次")
	}
	code := rand.Intn(9000) + 1000

	//sms, _ := pkg.SendSms(in.Account, strconv.Itoa(code))
	//if *sms.Body.Code != "OK" {
	//	return nil, fmt.Errorf(*sms.Body.Message)
	//}

	redisinit.Client.Set("SendSms"+in.Account+in.Source, code, time.Minute*10)

	redisinit.Client.Set("Send"+in.Account+in.Source, 1, time.Minute*1)

	return &sendrpc.SendSmsResponse{}, nil
}
